﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;
using Dapper_Core.BLL.Impl;
using Dapper_Core.BLL.Interface;
using Dapper_Core.Common;
using Dapper_Core.DAL.Impl;
using Dapper_Core.Model;
using Dapper_Core.Table;
using Dapper_Core.Web.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
using Newtonsoft.Json;

namespace Dapper_Core.Web.Controllers
{
    [TypeFilter(typeof(IgnoreCheckSessionFilter))]
    public class LoginController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Index(string user, string password)
        {
            if (!ValidateLogin(user,password))
            {
                ViewData["Err"] = "用户名或密码错误！";
                return View("Index");
            }
            SaveSession(user, password);
            NlogUtility.WriteInfo(MethodBase.GetCurrentMethod().DeclaringType.FullName, "用户登录成功", "user=" + user, "DapperCore");
            return RedirectToAction("Index", "Home");
        }

        public IActionResult NoPermission()
        {
            return View();
        }

        private bool ValidateLogin(string user, string password)
        {
            try
            {
                Expression<Func<Sys_User, bool>> exp = a => a.Code == user;
                List<Sys_User> userList = new Sys_UserBusiness().GetList(exp).ToList();
                if (password == "12345" && userList != null && userList.Count > 0) return true;
                NlogUtility.WriteInfo(MethodBase.GetCurrentMethod().DeclaringType.FullName, "校验用户登录信息失败但未出现异常", "user=" + user, "DapperCore");
                return false;
            }
            catch (Exception ex)
            {
                NlogUtility.WriteFileLog("校验用户登录信息异常user=" + user + "，ex=" + ex.ToString());
                NlogUtility.WriteError(MethodBase.GetCurrentMethod().DeclaringType.FullName, "校验用户登录信息异常", "user=" + user, ex, "DapperCore");
                return false;
            }
        }

        private void SaveSession(string user, string password)
        {
            SessionUtility.InitialSession();
            //保存当前登录人基本信息
            ISys_UserBusiness userBusiness = new Sys_UserBusiness();
            BasicUserInfo basicUserInfo = userBusiness.GetBasicUserInfo(user);
            basicUserInfo.RemoteIpAddress = IPUtility.GetVisitorRealIP();
            SessionUtility.SetBasicUserInfo(basicUserInfo);
            //保存当前用户所有类型的资源Url
            ISys_MenuBusiness menuBusiness = new Sys_MenuBusiness();
            List<string> pageUrls = menuBusiness.GetAllMenuPageUrls(user, RoleCode.OrdinaryUser);
            SessionUtility.SetSessionData(SessionKey.GetAllMenuPageUrlsKey(), pageUrls);
            //保存当前用户所有类型的资源Code
            List<string> menusCode = menuBusiness.GetAllMenusCode(user, RoleCode.OrdinaryUser);
            SessionUtility.SetSessionData(SessionKey.GetAllMenusCodeKey(), menusCode);
        }
    }
}